﻿using System.Data.SqlClient;
using CoreLibrary.Sql;

namespace Entities.DataBase.Sql {

    internal class UpdateBeforeDelete : ISqlUpdator<bool> {

        private readonly int _width;
        private readonly int _right;
        private readonly string _tableName;

        public UpdateBeforeDelete(int width, int right, string tableName) {
            _width = width;
            _right = right;
            _tableName = tableName;
        }

        public string GetSQL() {
            return string.Format(
               @"
                 UPDATE {0} SET RID = RID - @width WHERE RID > @right;
                 UPDATE {0} SET LID = LID - @width WHERE LID > @right;
               ",
                _tableName
            );
        }

        public void SetParameters(SqlCommand command) {
            command.Parameters.AddWithValue("@width", _width);
            command.Parameters.AddWithValue("@right", _right);
        }

        public bool GetResult(SqlConnection conn, int result) {
            return result >= 0;
        }
    }
}